const api = require("../../utils/cloudAPIUtils");
const app = getApp();

Page({
  data: {
    lists:[1,5,10,20,30,40,50,100,0.1],
    info:{},
    flag:true,
    isBack:true,
    deviceSn:'',
    slot:0,
    modalName:null,
    getOutPrice:0,
    hasMarket:app.globalData.HasMarket
  },

    hideModal(){
        this.setData({
            modalName:null
        })
    },
    showModal(e){
        this.setData({
            modalName:e.currentTarget.dataset.str
        })
    },
    btnChangeInput(e){
        this.setData({
            getOutPrice:e.detail.value
        })
    },
    btnAllGetOut(){
        this.setData({
            getOutPrice:this.data.info.money
        })
    },

    btnWalletGetOut(){
        const getOutPrice = parseFloat(this.data.getOutPrice);
        const self = this;
        if(getOutPrice<=0 || getOutPrice>parseFloat(this.data.info.money||0)){
            self.setData({
                modalName:null
            })
            wx.showModal({
                title: '提现失败',
                content: '提现金额必须大于0，并且小于当前余额',
                showCancel:false,
            })
            return;
        }
        api.post(api.url.walletGetOut,{
            price:getOutPrice
        },res=>{
            console.log(res)
            if(res.data.code==200){
                wx.showToast({
                    title: '申请提交成功！',
                    icon:'none'
                })
                self._init();
            }else{
                wx.showModal({
                  title: '提现失败',
                  content: res.data.msg,
                  showCancel:false
                })
            }
            self.setData({
                modalName:null,
                getOutPrice:0
            })
        })
    },

    // toBack(){
    //     wx.navigateBack();
    // },
    toDevPage(){
        const {deviceSn,slot} = this.data;
        wx.redirectTo({
            url:`/packageA/pages/cdcz/select2/select?deviceSn=${deviceSn}&num=${slot}`
        })
    },

  btnPay(e){
    if(!this.data.flag) return;
    const num = e.currentTarget.dataset.num;
    let self = this;
    this.setData({
      flag:false
    })
    wx.showModal({
      title:'充值提示',
      content:`确定充值 ${num} 元吗`,
      success(res){
        if(res.cancel) return; //点击取消则中止方法
        self._pay(num)
      },
      complete(){
        self.setData({
          flag:true
        })
      }
    })
  },
  _pay(num){
    let self = this;
    wx.showLoading({
      title: '加载中...',
    })
    api.post(api.url.walletAdd,{
      memberId:app.globalData.userInfo.memberId,
      money:num,
      appId:wx.getAccountInfoSync().miniProgram.appId,
      payType:3
    },(res)=>{
      wx.hideLoading()
      console.log(res)
      if(res.data.code==200){
        const payInfo = JSON.parse(res.data.body.payInfo);
        wx.requestPayment({
          appId:payInfo.appId,
          nonceStr: payInfo.nonceStr,
          package: payInfo.package,
          paySign: payInfo.sign,
          timeStamp: payInfo.timeStamp+"",
          signType:payInfo.signType,
          success(res){
            if(self.data.isBack){
              wx.showToast({
                title: `成功充值${num} 元!`,
                icon:'success',
                mask:true
              })
              const info = self.data.info;
              info.money = parseInt((parseFloat(self.data.info.money)+parseFloat(num))*100)/100;
              self.setData({info});
            }else{
                wx.showModal({
                  content: `成功充值${num} 元!`,
                  showCancel:false,
                  complete: (res) => {
                    app.globalData.deviceInfo = {};
                    self.toDevPage();
                  }
                })
            }
          },fail(err){
            wx.showToast({
              title: '支付失败',
              icon:'error',
              mask:true
            })
          }
        })
      }else{
        wx.showModal({
          title:'充值失败',
          content:res.data.msg,
          showCancel:false
        })
      }
    })
    // console.log("成功充值 "+num+" 元")
    // return true;
  },

  toHistoryList(){
    wx.navigateTo({
      url: "./list/list",
    })
  },

  _init(){
    const self = this;
    api.post(api.url.walletGet,{
        memberId:app.globalData.userInfo.memberId
      },(res)=>{
        if(res.data.code==200){
          self.setData({
            info:res.data.body
          })
        }
      })
  },

  onLoad(options){    
    if(options && options.deviceSn){
      this.setData({
        isBack:false,
        deviceSn:options.deviceSn,
        slot:options.slot
      })
    }
    this._init();
  }
})